package com.zeng.jiudian.utils;

import java.sql.*;

/**
 * @Author JJ_yo
 * @Date 2020/7/16-14:44
 */
public class JDBC
{
    /**
     * 数据库连接信息
     */
    private static final String URL = "jdbc:mysql://localhost:3306/jiudian?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123567";

    /**
     * 数据库需要的对象
     */
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet;

    /**
     * 获取连接对象
     *
     * @return 连接对象
     */
    public static Connection getConnection()
    {
        try
        {
            Class.forName(DRIVER);
            if (connection == null)
            {
                connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return connection;
    }

    /**
     * 增删改方法
     *
     * @param sql
     * @return 影响行数
     */
    public static int executeUpdate(String sql)
    {
        connection = getConnection();
        int affect = 0;
        try
        {
            preparedStatement = connection.prepareStatement(sql);
            affect = preparedStatement.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            close();
        }

        return affect;
    }

    /**
     * 查询方法
     *
     * @param sql
     * @return 结果集
     */
    public static ResultSet getResultSet(String sql)
    {
        connection = getConnection();
        try
        {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return resultSet;
    }

    /**
     * 释放资源
     */
    public static void close()
    {
        if (connection != null)
        {
            try
            {
                connection.close();
                connection = null;
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        if (preparedStatement != null)
        {
            try
            {
                preparedStatement.close();
                preparedStatement = null;
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }

        if (resultSet != null)
        {
            try
            {
                resultSet.close();
                resultSet = null;
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
}
